﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;

/*
 * 采用接口方式，不使用继承EventTrigger的方式
 * 原因：继承EventTrigger，会导致UGUI原生实现了EventTrigger的UI自身事件失效，也就是与EventTrigger
 * 冲突了，例如ScrollView无法滚动
 */

public class EventTriggerListener : MonoBehaviour, IPointerClickHandler,IPointerDownHandler
    ,IPointerUpHandler,IPointerEnterHandler,IPointerExitHandler,ISelectHandler
    ,IDeselectHandler,IUpdateSelectedHandler
{
    public delegate void VoidDelegate(GameObject go);
    public delegate void BoolDelegate(GameObject go, bool state);
    public delegate void FloatDelegate(GameObject go, float delta);
    public delegate void VectorDelegate(GameObject go, Vector2 delta);
    public delegate void ObjectDelegate(GameObject go, object  userData);
    public delegate void KeyCodeDelegate(GameObject go, KeyCode key);

    public VoidDelegate onClick;
    public VoidDelegate onDoubleClick;
    public VoidDelegate onDown;
    public VoidDelegate onEnter;
    public VoidDelegate onExit;
    public VoidDelegate onUp;
    public VoidDelegate onSelect;
    public VoidDelegate onDeselect;
    public VoidDelegate onUpdateSelect;

    public ObjectDelegate onClick_Obj;
    public object userData;

    static public EventTriggerListener Get(GameObject go)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if (listener == null) listener = go.AddComponent<EventTriggerListener>();
        return listener;
    }

    static public EventTriggerListener Get(Transform transform)
    {
        EventTriggerListener listener = transform.GetComponent<EventTriggerListener>();
        if (listener == null) listener = transform.gameObject.AddComponent<EventTriggerListener>();
        return listener;
    }

    private void Update()
    {        
    }


    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
        {
            if (onClick != null && eventData.clickCount == 1)
            {
                onClick(gameObject);
            }
            if (onDoubleClick != null && eventData.clickCount == 2)
            {
                onDoubleClick(gameObject);
            }
            if (onClick_Obj != null && eventData.clickCount == 1)
            {
                onClick_Obj(gameObject, userData);
            }
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if (onExit != null) onExit(gameObject);
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if(onEnter != null) onEnter(gameObject);
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (onUp != null && eventData.button == PointerEventData.InputButton.Left)
        {
            onUp(gameObject);
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        if (onDown != null && eventData.button == PointerEventData.InputButton.Left)
        {
            onDown(gameObject);
        }
    }

    public void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null) onSelect(gameObject);
    }

    public void OnDeselect(BaseEventData eventData)
    {
        if (onDeselect != null) onDeselect(gameObject);
    }

    public void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelect != null) onUpdateSelect(gameObject);
    }

}